using System.Windows;
using System.Windows.Media;
using HandyControl.Controls;

namespace HandyControlUIDemo.Windows;

public class CustomHcPasswordBox:PasswordBox
{
    private TextBox _textBox;

    public TextBox InternalTextBox
    {
        get
        {
            if (_textBox == null)
            {
                _textBox = FindVisualChild<TextBox>(this);
            }

            return _textBox;
        }
    }

    private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            if (child is T typedChild)
            {
                return typedChild;
            }

            var result = FindVisualChild<T>(child);

            if (result != null)
            {
                return result;
            }
        }

        return null;
    }
}